import SwiftUI

// LookPage
// 看一看页面

struct LookPage: View {
    @State private var searchInput: String = ""
    
    @StateObject private var lookFetcher = BookLookFetcher()
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                    LookSearch(searchInput: $searchInput)
                }
                
                LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 2)) {
                    ForEach(lookFetcher.publishedResult.collection, id:\.bookId) { item in
                        NavigationLink {
                            LookDetail(book: item)
                        } label : {
                            LookGridItem(book: item)
                        }
                    }
                }
            }
            .refreshable {
                lookFetcher.fetch(query: searchInput)
            }
        }
        .task {
            lookFetcher.fetch(query: searchInput)
        }
        .onChange(of: searchInput) {
            lookFetcher.fetch(query: searchInput)
        }
    }
}

#Preview {
    LookPage()
}
